package fr.controller;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

import fr.beans.User;
import fr.iservice.dao.IServiceDaoUser;


/**
 * @author Caroline Lavaure
 * Project : Caroline aloe vera
 * 11 juil. 2013
 */
public class UserAction extends ActionSupport implements SessionAware {
	
	/* ************************************* */
	/*               PROPERTIES              */
	/* ************************************* */
	/**
	 * the field long.
	 */
	private static final long serialVersionUID = 6320957679584062325L;

	/**
	 * the field String.
	 */
	private String mdp, login;

	// Objets
	/**
	 * the field User.
	 */
	private User user;

	// Services.
	/**
	 * the field IServiceDao<User>.
	 */
	private IServiceDaoUser userService;

	// SESSION 
	/** 
	 * the field Map<String,Object>.
	 */
	private Map<String, Object> session;

	/* ************************************* */
	/*               METHODS                 */
	/* ************************************* */

	/**
	 * @return Success or input
	 */
	public final String seConnecter() {
			
		login = user.getIdentifiant();
		mdp   = user.getMotDePasse();
		
		User lUser = userService.readUser(login, mdp);

		if (lUser == null) {

			addFieldError("user.nom", "Login non trouve");

			return "input";

		} else {

			// suppression de la valeur du mot de passe dans user.
			lUser.setMotDePasse(null); 

			//sauvegarde du user dans la session
			session.put("user", lUser);
		}
		return SUCCESS;
	}

	
	/**
	 * @return Success. 
	 */
	public final String seDeconnecter() {

		session.remove("user");

		return SUCCESS;

	}



	/* ************************************* */
	/*       GETTER & SETTERS                */
	/* ************************************* */

	/* ------- USER ------- */ 
	/**
	 * @return the user
	 */
	public final User getUser() {
		return user;
	}
	/**
	 * @param puser .
	 */
	public final void setUser(final User pUser) {
		this.user = pUser;
	}


	/* ------- userService ------- */ 

	/**
	 * @return the userService
	 */
	public final  IServiceDaoUser getUserService() {
		return userService;
	}


	/**
	 * @param puserService .
	 */
	public final  void setUserService(IServiceDaoUser puserService) {
		this.userService = puserService;
	}


	/* ------- SESSION ------- */ 
	@Override
	public void setSession(final Map<String, Object> pSession) {
		this.session = pSession;
	}


//	/** (non-Javadoc).
//	 * @see com.opensymphony.xwork2.Preparable#prepare()
//	 * 
//	 */
//	@Override
//	public void prepare() throws Exception {
//		UtilTimerStack.setActive(true);
//		
//	}


	



	






}



